<?php
// +----------------------------------------------------------------------
// | 随访系统
// +----------------------------------------------------------------------
// | Copyright (c) 2018-2050 http://www.hedesoft.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +---------------------------------------------------------------------
// | Author: 小李子 < 490959289@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Validate;
class Category extends Base
{
    public function index()
    {
        $key = $this->request->param('key');
        $where = array();
        if ($key){
             $where['category'] = array('LIKE', '%'.$key.'%');
        }else{
            $where['pid'] = 0;
        }
        $list=model('Category')->where($where)->order('sort desc')->paginate(5);
        // 获取分页显示
        $page = $list->render();
        $this->assign('list', $list);
        $this->assign('page', $page);
    	return $this->fetch();
    }
    //加载添加/编辑页
    public function add_category(){
    $model=model('Category');
    $id = $this->request->param('id');
    $menu = $model->where('id',$id)->find();
    $this->assign('data', $menu);
    $list=$model->where('pid',0)->select();
    $this->assign('list', $list);
    return $this->fetch();	
    }

    //数据保存
    public function save_data(){
        $model=model('Category');

        $rule = [
        'category'  => 'require|max:255',
        ];

        $msg = [
            'category.require' => '分类名称不能为空'
        ];

        $data['category'] = $this->request->param('category');
        $data['icon'] = $this->request->param('icon');
        $data['is_show'] = $this->request->param('is_show');
        $data['sort'] = $this->request->param('sort');
        $data['pid'] = $this->request->param('pid');
        $validate = new Validate($rule, $msg);
        if (!$validate->check($data)){
            echo json_encode(['status'=>0,'msg'=>$validate->getError()]);exit();
        }


        $id = $this->request->param('id');
        if ($id){
            $status = $model->where('id',$id)->update($data);
        }else{

            $data['add_time'] = time();
            $status = $model->insertGetId($data);
        }
        if ($status){
            echo json_encode(['status'=>1,'msg'=>'保存成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'保存失败']);exit();
        }
    }

    //删除菜单
    public function delCate()
    {
        $model=model('Category');
        $data['id'] = $this->request->param('id');
        $data['pid'] = $this->request->param('id');
        $menu_status = $model->where($data)->delete();
        if ($menu_status){
            echo json_encode(['status'=>1,'msg'=>'删除成功']);exit();
        }else{
            echo json_encode(['status'=>0,'msg'=>'删除失败']);exit();
        }
    }
}
